<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/10/29 0029
 * Time: 17:25
 */

namespace app\index\controller;
use app\index\model\Email;
use app\index\model\Sign;
use app\index\model\User;
use PHPMailer\Mail;
use think\Controller;
use think\Db;

class UserCenter extends Controller
{

	/**
	 * 用户中心主页
	 * @return mixed
	 */
	public function index(){
		$res = Sign::get(['user_id'=>session('user_id')]);
		if ($res && time() - $res->sign_time <=86400){
			$this->assign('time',time());
		}
		$this->assign('sign',$res);
		$this->assign('title','用户中心');
		return $this->fetch();
	}


	/**
	 * 用户中心签到方法
	 * @throws \think\exception\DbException
	 */
	public function sign(){
		if (!session('user_id')){
			$this->error('请先登陆');
		}
		$post = input('post.');
		$res = Sign::get(['user_id'=>session('user_id')]);
		if ($res){
			if (time() - $res->sign_time >= 172800){
				$ok = Sign::where('user_id', session('user_id'))
					->update(['sign_num' => 1,'sign_time'=>time()]);
				if ($ok){
					Db::name('user')->where('user_id', session('user_id'))->setInc('kiss', 20);
					$this->success('签到成功');
				}else{
					$this->error('签到失败，请稍后再试');
				}
			}
			if (time() - $res->sign_time>=86400 && time() - $res->sign_time<=172800){
				Sign::where('user_id', session('user_id'))
					->update(['sign_time' => time()]);
				Db::name('user')->where('user_id', session('user_id'))->setInc('kiss', 20);
				Db::name('sign')->where('user_id', session('user_id'))->setInc('sign_num', 1);
			}else{
				$this->error('你今天已经签到过了');
			}
		}else{
			$post['user_id'] = session('user_id');
			$post['sign_time'] = time();
			$post['sign_num'] = 1;
			$ok = Sign::create($post);
			if ($ok){
				Db::name('user')->where('user_id', session('user_id'))->setInc('kiss', 20);
				$this->success('签到成功');
			}else{
				$this->error('签到失败，请稍后再试');
			}
		}
	}


	/**
	 * 激活邮箱页面
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function active(){
		$active = User::get(session('user_id'));
		$this->assign('is_active',$active);
		return $this->fetch();
	}


	/**
	 * 发送邮箱内容
	 * @throws \think\exception\DbException
	 */
	public function activation(){
		$user = User::get(session('user_id'));
		if ($user->is_active==1){
			$this->error('你已经激活了');
		}
		if (!$user->email){
			$this->error('你还没有填写邮箱,请先填写邮箱');
		}
		$token = md5(date('YmdHis').'?'.mt_rand(1,100000));
		$nickname = $user->nickname;
		$url = 'http://www.flyx.com'.url('index/UserCenter/checkEmail',['token'=>$token]);
		$content = <<<xbs
        <div><includetail><table border="0" cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555; font-family: 'Helvetica Neue Regular',Helvetica,Arial,Tahoma,'Microsoft YaHei','San Francisco','微软雅黑','Hiragino Sans GB',STHeitiSC-Light; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;"> <tbody style="margin: 0; padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;" href="http://fly.layui.com/" target="_blank">Fly社区 - Layui前端框架官方社区</a></div> </td> </tr> <tr style="margin: 0; padding: 0;"> <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight: 700;">{$nickname}</em>，请完成以下操作： </p> <div style=""> <a href="{$url}" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank">立即激活邮箱</a> </div> <p style="line-height: 20px; margin-top: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"> <td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table></includetail></div>
xbs;
		$res = Mail::sendMail($user->email,'激活邮箱',$content);
		if (!$res){
			$this->error('邮箱发送错误');
		}else{
			$data['token'] = $token;
			$data['create_time'] = time();
			$data['email'] = $user->email;
			Email::create($data);
			$this->success('邮件已发送,请注意查收');
		}
	}


	/**
	 * 邮件内容，确认是否激活
	 * @throws \think\exception\DbException
	 */
	public function checkEmail(){
		$get = input('token');
		$email = Email::get(['token'=>$get]);
		if (!$email){
			$this->error('对不起,激活邮箱出现错误');
		}
		$a = strtotime($email->create_time);
		if ($a <= time()-1800){
			$this->error('链接已失效,请重新获取');
		}else{
			User::where('user_id',session('user_id'))->update(['is_active'=>1]);
			header("Location:http://www.flyx.com/index/user_center/active.html");
		}
	}
}